package zhieasy.cn.service.found.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhieasy.cn.common.utils.ResultVOUtil;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.common.StudentUser;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.model.found.LostAndFind;
import zhieasy.cn.model.found.Opinion;
import zhieasy.cn.persistence.mapper.common.StudentUserMapper;
import zhieasy.cn.persistence.mapper.common.WeChatUserMapper;
import zhieasy.cn.persistence.mapper.found.LostAndFindMapper;
import zhieasy.cn.persistence.mapper.found.OpinionMapper;
import zhieasy.cn.service.found.UserService;

/**
 * @author wangzeya
 * @create 2020-08-02 17:10
 */
@Service

public class UserServiceImpl implements UserService {

    @Autowired
    private LostAndFindMapper lostAndFindMapper;
    @Autowired
    private WeChatUserMapper weChatUserMapper;
    @Autowired
    private StudentUserMapper studentUserMapper;
    @Autowired
    private OpinionMapper opinionMapper;

    @Override
    public ResultVO createUser(WechatUser weChatUser) {
        weChatUserMapper.insert(weChatUser);
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO completeUser(WechatUser weChatUser) {
        weChatUserMapper.update(weChatUser, null);
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO<WechatUser> findUserByOpenid(String openId) {
        QueryWrapper<WechatUser> wrapper = new QueryWrapper<>();
        wrapper.eq("wx_unionid", openId);
        WechatUser weChatUser = weChatUserMapper.selectOne(wrapper);
        return ResultVOUtil.success(weChatUser);
    }

    @Override
    public ResultVO<LostAndFind> findMyLaf(int pageNum, int pageSize, String openId) {
        IPage<LostAndFind> lostAndFindPage = new Page<>(pageNum, pageSize);//参数一是当前页，参数二是每页个数
        QueryWrapper<LostAndFind> wrapper = new QueryWrapper<>();
        wrapper.eq("union_id", openId);
        IPage<LostAndFind> lostAndFindIPage = lostAndFindMapper.selectPage(lostAndFindPage, wrapper);
        return ResultVOUtil.success(lostAndFindIPage);

    }

    @Override
    public ResultVO createStuCert(StudentUser studentUser) {
        studentUserMapper.insert(studentUser);
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO createOpinion(Opinion opinion) {
        opinionMapper.insert(opinion);
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO completeLaf(String lafId) {
        LostAndFind lostAndFind = new LostAndFind();
        lostAndFind.setId(lafId);
        lostAndFind.setLafActive(1);
        lostAndFindMapper.updateById(lostAndFind);
        return ResultVOUtil.success();
    }

    @Override
    public int isCompeteUser(String openId) {
        int i = 0;
        StudentUser userById = studentUserMapper.selectById(openId);

        if (userById!=null && userById.getStudentEmail()!=null && userById.getStudentEmail().length() > 0){
            i = 1;
        }
        return i;
    }

    @Override
    public int isStuCert(String openId) {
        int i = 0;
        QueryWrapper<StudentUser> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", openId);
        StudentUser stuCert = studentUserMapper.selectOne(wrapper);
        if (stuCert != null){
            i = 1;
        }
        return i;
    }

    @Override
    public ResultVO<StudentUser> findStuByUserId(String openId) {
        QueryWrapper<StudentUser> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_id", openId);
        StudentUser stuCert = studentUserMapper.selectOne(wrapper);
        return ResultVOUtil.success(stuCert);
    }

    @Override
    public  ResultVO<WechatUser>  findUserByUserId(String openId) {
        WechatUser weChatUser = weChatUserMapper.selectById(openId);
        return ResultVOUtil.success(weChatUser);
    }

    @Override
    public ResultVO modifyWeChatUser(WechatUser weChatUser) {
        //查看原用户信息（应为只能修改 电话和邮箱）
        WechatUser userById = weChatUserMapper.selectById(weChatUser.getWxOpenId());
        //创建新的用户信息
        userById.setWxPhone(weChatUser.getWxPhone());
//        userByIdzzzz(weChatUser.getUserEmail());
        //todo 修改次数从前端修改？
//        userById.setUpdateNum(userById.getUpdateNum()+1);
//        userById.setuz(weChatUser.getUpdateNum());
        //修改用户信息
        weChatUserMapper.updateById(userById);
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO modifyStuCertification(StudentUser studentUser) {
        //查看原学生信息
        StudentUser stuCertByUserId = studentUserMapper.selectById(studentUser.getOpenId());
        //完善新学生信息
        studentUser.setOpenId(stuCertByUserId.getOpenId());
        studentUser.setCreateTime(stuCertByUserId.getCreateTime());
        //修改学生认证
        studentUserMapper.updateById(studentUser);
        return ResultVOUtil.success();
    }
}
